package com.xiaoc.bulldozer.controller;


import com.xc.framework.common.exception.BusinessException;
import com.xc.framework.common.oss.AliOSS;
import com.xiaoc.bulldozer.annotate.Duplicate;
import com.xiaoc.bulldozer.common.ConfigKeys;
import com.xiaoc.bulldozer.common.Constant;
import com.xiaoc.bulldozer.common.ImageConstant;
import com.xiaoc.bulldozer.common.ResponseCode;
import com.xiaoc.bulldozer.common.dto.WeixinUserDto;
import com.xiaoc.bulldozer.common.model.Account;
import com.xiaoc.bulldozer.common.service.AccountService;
import com.xiaoc.bulldozer.common.service.cache.CommonConfigCacheService;
import com.xiaoc.bulldozer.facade.PosterFacade;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping(value = "/material")
public class MaterialController extends BulldozerFrontBaseController {

    private static final Logger logger = LoggerFactory.getLogger(MaterialController.class);

    @Resource(name = "commonConfigService")
    private CommonConfigCacheService commonConfigService;

    @Resource(name = "accountService")
    private AccountService accountService;

    @Resource
    private AliOSS aliOss;

    @Resource(name = "posterFacade")
    private PosterFacade posterFacade;

    private String codePagePath = "pages/index/index";


    
    /**
     * 图片域名
     */
    @Value("${oss.image.domain}")
    private String IMAGE_DOMAIN;

    private static final String separator = "/";

    @Duplicate
    @PostMapping(value = "/friendCode")
    public ModelAndView generateWxCodeFile() {

        WeixinUserDto weixinUserDto = getCurrentWeixinUserDto();

        Account account = accountService.getAccountByUserId(weixinUserDto.getId());

        if (account == null) {
            throw new BusinessException(ResponseCode.BUSINESS_ERROR.getCode(), "请先注册");
        }

        boolean isChange = false;
        if (StringUtils.equals(commonConfigService.getValue(ConfigKeys.POSTER_FORCE_CHANGE), String.valueOf(Constant.ENABLE))) {
            isChange = true;
        }

        posterFacade.generateWxCodeFile(isChange, String.valueOf(weixinUserDto.getId()), ImageConstant.WX_WXCODE_FOLDER_FOR_FRIEND, String.valueOf(account.getPhoneNumber()), codePagePath, 800, 0, 0, 0, false);

        Map<String, Object> params = new HashMap<>();
        String imgUrl = IMAGE_DOMAIN+ImageConstant.WX_WXCODE_FOLDER_FOR_FRIEND+separator+String.valueOf(account.getPhoneNumber())+ImageConstant.IMAGE_SUFFIX;
        params.put("imgUrl",imgUrl);
        params.put("bgImgUrl",commonConfigService.getValue(ConfigKeys.SHARE_CIRCLE_FRIEND_BG_IMG));
        logger.info("小程序码图片地址{}",imgUrl);

        return renderJson("picture/friendCode.json", params);

    }



}
